﻿using System;
using System.Collections.ObjectModel;
using MVVM.Repository;
using MVVM.Model;
using System.ComponentModel;
using GalaSoft.MvvmLight.Messaging;

namespace MVVM.ViewModel {
    public class CustomersViewModel : ViewModel,INotifyPropertyChanged {

        #region Entity list and constructor
        
        CustomerRepository repository;
        ObservableCollection<Customer> listOfCustomers;
        Customer selectedCustomer;

        public CustomersViewModel() {
            if (repository == null) {
                repository = new CustomerRepository();
            }
            ListOfCustomers = new ObservableCollection<Customer>(repository.GetAllCustomers());
            this.SearchText = "Some text to search ...";
        }

        public ObservableCollection<Customer> ListOfCustomers {
            
            get { return listOfCustomers; }
            private set {
                if (listOfCustomers != value) {
                    listOfCustomers = value;
                    NotifyPropertyChanged("ListOfCustomers");
                }
            }
        }

        public Customer SelectedCustomer {
            get { return selectedCustomer; }
            set 
            {
                if (selectedCustomer != value) {
                    selectedCustomer = value;
                    NotifyPropertyChanged("SelectedCustomer");
                }
            }
        }

        public string SearchText { get; set; }

        #endregion

        #region Relay Commands
        public ViewCommand newCommand;
        public ViewCommand NewCommand {
            get {
                if (newCommand == null)
                    newCommand = new ViewCommand(param => this.NewCustomer());
                return newCommand;
            }
        }
        private void NewCustomer() {
            NavigationActions.OpenCustomerView(SelectedCustomer);
        }

        private ViewCommand editCommand;
        public ViewCommand EditCommand {
            get {
                if (editCommand == null) {
                    editCommand = new ViewCommand(param => this.EditCustomer());
                }
                return editCommand;
            }
        }
        private void EditCustomer() {
            NavigationActions.OpenCustomerView();
        }

        private ViewCommand saveCommand;
        public ViewCommand SaveCommand {
            get {
                if (saveCommand == null) {
                    saveCommand = 
                        new ViewCommand(
                            param => this.SaveCustomers(), 
                            param => this.CanSaveCustomers
                            );
                }
                return saveCommand;
            }
        }
        public void SaveCustomers() {
            repository.Commit();
        }
        private bool CanSaveCustomers {
            get { return true; }
        }

        private ViewCommand deleteCommand;
        public ViewCommand DeleteCommand {
            get {
                if (deleteCommand == null) {
                    deleteCommand = new ViewCommand(
                        param=>this.DeleteCustomer(),
                        param=>this.CanDeleteCustomer
                        );
                }
                return deleteCommand;
            }
        }
        public void DeleteCustomer() {
            if (SelectedCustomer != null) {
                if(NavigationActions.QueryConfirmation(
                    "Delete Customer.",
                    string.Format(
                    "Do you want to delete {0} {1}?",
                    SelectedCustomer.FirstName,
                    selectedCustomer.LastName))){
                    //ListOfCustomers.Remove(SelectedCustomer);
                    //repository.DeleteCustomer(SelectedCustomer);
                    ListOfCustomers[ListOfCustomers.IndexOf(SelectedCustomer)].FirstName = "***DELETED***";
                }
            } else {
                NavigationActions.ShowError("Delete Customer.", "You must select a Customer!");
            }
        }
        private bool CanDeleteCustomer {
            get { return true; }
        }

        private ViewCommand ordersCommand;
        public ViewCommand OrdersCommand {
            get {
                if (ordersCommand == null) {
                    ordersCommand = new ViewCommand(param=>this.ViewCustomerOrders());
                }
                return ordersCommand;
            }
        }
        private void ViewCustomerOrders() { }

        private ViewCommand findCommand;
        public ViewCommand FindCommand {
            get {
                if (findCommand == null) {
                    findCommand = new ViewCommand(param => this.FindCustomers());
                }
                return findCommand;
            }
        }
        private void FindCustomers() {
            if (this.SearchText == string.Empty || this.SearchText == null) {
                NavigationActions.ShowError("Search Customers.", "Please enter some text ...");
                return;
            }
            ListOfCustomers = new ObservableCollection<Customer>(
                repository.GetCustomersByQuery(
                p => p.CompanyName.StartsWith(SearchText)
                || p.FirstName.StartsWith(SearchText)
                || p.LastName.StartsWith(SearchText)));
        }

        private ViewCommand exitCommand;
        public  ViewCommand ExitCommand {
            get {
                if (exitCommand == null) {
                    exitCommand = new ViewCommand(param => this.ExitWindow());
                }
                return exitCommand;
            }
        }
        private void ExitWindow() {
            NavigationActions.CloseCurrentView();
        }
        #endregion


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String info) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }
}
